package com.java.exer1;

import org.junit.Test;

/**
 * 获取两个字符串中最大的相同子串。比如：
 * str1="abcwerthelloyuiodef"  str2="cvhellobnm"
 * 提示： 将长度短的那个字符串进行长度依次递减的子串与较长的字符串比较
 *
 * @author shkstart
 * @create 2021-01-05 23:03
 */
public class StringTest_5 {
    @Test
    public void test1() {
        String s1 = new String("abcwerthelloyuiodef");
        String s2 = new String("cvhellobnm");
        String a = serch1(s1, s2);
        System.out.println(a);


    }

    public String serch1(String st1, String st2) {
        if (st1 != null && st2 != null) {

            String maxString = (st1.length() >= st2.length()) ? st1 : st2;
            String minString = (st1.length() < st2.length()) ? st1 : st2;
            int length = minString.length();

            for (int i = 0; i < length; i++) {
                for (int x = 0, y = length - i; y <= length; x++, y++) {
                    String subStr = minString.substring(x, y);
                    if (maxString.contains(subStr)) {
                        return subStr;
                    }

                }
            }

        }
        return null;

    }
}
